﻿
Imports VSModel
Imports System.Data
Imports Cls_Common

Partial Class FrontEnd_Control_ShoppingCart
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'hien thi thong tin
        If Not Page.IsPostBack Then
            ShowShoppingCartList()
            CalculateSumPrice()
            btnCheckOut.Attributes.Add("onClick", "javascript: return ThanhToan();")
            '    AddShoppingCart(3, 4, 1)
        End If
    End Sub
    Private Sub AddShoppingCart(ByVal _ProductID As String, ByVal _StoreId As Integer, ByVal quantity As Integer)
        'Dim cls As New clsDACart
        'Dim dt As New DataTable
        'cls.CartID = Session.SessionID()
        'cls.ProductID = Request("ProductID")
        'cls.Quantity = quantity
        'cls.Insert_ShoppingCart()
        'cls.Destructor()
        'cls = Nothing
        Using Data As New VSEntities
            Dim p As New VSModel.ShoppingCart
            p.CartId = Session.SessionID
            p.ProductId = _ProductID
            p.UserId = 6 'Session("UserId")
            p.StoreId = _StoreId

            p.Quantity = quantity
            Data.ShoppingCarts.AddObject(p)
            Data.SaveChanges()
        End Using


    End Sub

    Private Sub ShowShoppingCartList()
        Using Data As New VSEntities
            Dim p = (From q In Data.ShoppingCarts Where q.CartId = Session.SessionID() Select q).ToList

            With ShpCartList
                .DataKeyField = "ProductID"
                .DataSource = p
                .DataBind()
            End With
        End Using
    End Sub

    Protected Function ImgPath(ByVal ImgSmall As string) As String
        If ImgSmall <> "" Then
            Return ImgSmall
        Else
            Return MainDomain & "NoImages.jpg"
        End If

    End Function

    Private Sub UpdateShoppingCart()
        ' Iterate through all rows within shopping cart list
        Dim i As Integer
        For i = 0 To ShpCartList.Items.Count - 1
            Dim remove As CheckBox = CType(ShpCartList.Items(i).FindControl("Remove"), CheckBox)
            Dim lblProductID As Label = CType(ShpCartList.Items(i).FindControl("lblProductID"), Label)
            ' If the quantity field is changed or delete is checked
            If remove.Checked = True Then
                If remove.Checked = True Then
                    RemoveItem(lblProductID.Text)
                End If
            End If
            ' update quantity
            Dim Qty As String = CType(ShpCartList.Items(i).FindControl("txtQuantity"), TextBox).Text
            If IsNumeric(Qty) Then
                If Qty <= 0 Then Qty = 1
                If Qty > 999 Then Qty = 999
                UpdateCart(lblProductID.Text, Qty)
                CalculateSumPrice()
            End If
        Next
    End Sub
    ' remove an item from shopping cart
    Private Sub RemoveItem(ByVal _ProductId As Integer)

        Using Data As New VSEntities
            Dim p As New VSModel.ShoppingCart
            p = (From q In Data.ShoppingCarts Where q.CartId = Session.SessionID And q.ProductId = _ProductId Select q).FirstOrDefault
            Data.ShoppingCarts.DeleteObject(p)
            Data.SaveChanges()
        End Using

    End Sub
    'Update cart
    Private Sub UpdateCart(ByVal _ProductId As Integer, ByVal Quantity As Integer)
  
        Using Data As New VSEntities
            Dim p As New VSModel.ShoppingCart
            p = (From q In Data.ShoppingCarts Where q.CartId = Session.SessionID And q.ProductId = _ProductId Select q).FirstOrDefault
            p.Quantity = Quantity
            Data.SaveChanges()
        End Using


    End Sub
    'tinh tong tien theo cartID
    Private Sub CalculateSumPrice()


        Using Data As New VSEntities
            Dim p As VSModel.CalculateSumPriceByCartID_Result = Data.CalculateSumPriceByCartID(Session.SessionID).FirstOrDefault
            If Not p Is Nothing AndAlso Not p.sumPrice Is Nothing Then
                lblTongTien.Text = FormatCurrency(p.sumPrice, ",", ".")
            End If

        End Using
    End Sub

    Protected Sub ShpCartList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles ShpCartList.ItemDataBound

        Dim row = CType(e.Item.DataItem, VSModel.ShoppingCart)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        Dim hplStore As HyperLink = CType(e.Item.FindControl("hplStore"), HyperLink)

        Dim lblPrice As Label = CType(e.Item.FindControl("lblPrice"), Label)
        Dim lblSumPrice As Label = CType(e.Item.FindControl("lblSumPrice"), Label)
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            ' If script return true: cAll Postback 

            Dim Qty As String = CType(e.Item.FindControl("txtQuantity"), TextBox).Text
            lblPrice.Text = FormatCurrency(row.Product.SellPrice, ",", ".")
            lblSumPrice.Text = FormatCurrency(row.Product.SellPrice * CInt(Qty), ",", ".")
            hplTitle.Text = row.Product.Title

            hplTitle.NavigateUrl = MainDomain & "san-pham-chi-tiet/" & row.Store.QueryString & "-" & row.Store.StoreId & "/" & row.Product.QueryString & "-" & row.Product.ProductId & ".html"
            'store
            hplStore.Text = row.Store.Title
            hplStore.NavigateUrl = MainDomain & row.Store.QueryString & "-" & row.Product.Store.StoreId & "/store.html"

        End If
    End Sub

    Protected Sub btnUpdate_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        UpdateShoppingCart()
        ShowShoppingCartList()
    End Sub


    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Using Data As New VSEntities
            Dim p As New VSModel.Order

            p.CartId = Session.SessionID()
            p.CustomerName = txtCustomerName.Text.Trim()
            p.CustomerAddress = txtCustomerAddress.Text.Trim()
            p.CustomerEmail = txtCustomerEmail.Text.Trim()
            p.CustomerPhone = txtCustomerPhone.Text.Trim()
            p.OrderDate = Now()
            p.StatusId = 1
            p.CustomerNote = txtCustomerNote.Text.Trim()

            Data.Orders.AddObject(p)
            Data.SaveChanges()


        End Using
        Response.Redirect(MainDomain & "Page/Thanks.aspx")

    End Sub
End Class
